<template>
  <div class="add-card">
    <header class="add-header">
      <el-page-header :content="title" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">车辆信息</div>
        <div class="form">
          <el-form ref="form1" label-width="100px" :rules="rules" :model="form1">
            <el-form-item label="车主姓名" prop="personName">
              <el-input v-model="form1.personName" place />
            </el-form-item>
            <el-form-item label="联系方式" prop="phoneNumber">
              <el-input v-model="form1.phoneNumber" />
            </el-form-item>
            <el-form-item label="车辆号码" prop="carNumber">
              <el-input v-model="form1.carNumber" />
            </el-form-item>
            <el-form-item label="车辆品牌" prop="carBrand">
              <el-input v-model="form1.carBrand" />
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div class="form-container">
        <div class="title">最新一次月卡缴费信息</div>
        <div class="form">
          <el-form ref="form2" label-width="100px" :rules="payRules" :model="form2">
            <el-form-item label="有效日期" prop="payTime">
              <el-date-picker
                v-model="form2.payTime"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>
            <el-form-item label="支付金额" prop="paymentAmount">
              <el-input v-model="form2.paymentAmount" />
            </el-form-item>
            <el-form-item label="支付方式" prop="paymentMethod">
              <el-select v-model="form2.paymentMethod">
                <el-option
                  v-for="item in paymentMethodList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"
                />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </main>
    <footer class="add-footer">
      <div class="btn-container">
        <el-button @click="resetBtn">重置</el-button>
        <el-button type="primary" @click="submitBtn">确定</el-button>
      </div>
    </footer>
  </div>
</template>

<script>
import { addCard, cardInfo, editCard } from '@/api/card'
export default {
  name: 'AddDept',
  data() {
    return {
      title: '',
      form1: {
        personName: '',
        phoneNumber: '',
        carNumber: '',
        carBrand: ''
      },
      form2: {
        payTime: '',
        paymentAmount: 0,
        paymentMethod: ''
      },
      paymentMethodList: [
        { id: 1, name: '支付宝' },
        { id: 2, name: '微信' },
        { id: 3, name: '现金' }
      ],
      payRules: {
        payTime: [{ required: true, message: '请输入有效日期', trigger: 'blur' }],
        cardEndDate: [{ required: true, message: '请输入有效日期', trigger: 'blur' }],
        paymentAmount: [{ required: true, message: '请输入支付金额', trigger: 'blur' }],
        paymentMethod: [{ required: true, message: '请输入支付方式', trigger: 'blur' }]
      },
      rules: {
        personName: [{ required: true, message: '请输入车主姓名', trigger: 'blur' }],
        phoneNumber: [{ required: true, message: '请输入联系方式', trigger: 'blur' }],
        carNumber: [{ required: true, message: '请输入车牌号码', trigger: 'blur' },
          { pattern: /^[\u4E00-\u9FA5][\da-zA-Z]{6}$/, message: '车牌号码格式不正确', trigger: 'blur' }
        ],
        carBrand: [{ required: true, message: '请输入车辆品牌', trigger: 'blur' }]
      }
    }
  },
  mounted() {
    if (this.$route.params.id) {
      this.title = '编辑月卡'
      this.getInfo(this.$route.params.id)
    } else {
      this.title = '添加月卡'
    }
  },
  methods: {
    async submitBtn() {
      await this.$refs.form1.validate()
      await this.$refs.form2.validate()
      const data = {
        ...this.form1,
        ...this.form2,
        cardStartDate: this.form2.payTime[0],
        cardEndDate: this.form2.payTime[1]
      }
      delete data.payTime
      if (!this.$route.params.id) {
        this.title = '添加月卡'
        await addCard(data)
        this.$message.success('添加成功')
      } else {
        await editCard(data)
        this.$message.success('编辑成功')
      }
      this.resetBtn()
      this.$router.back()
    },
    resetBtn() {
      this.$refs.form1.resetFields()
      this.$refs.form2.resetFields()
    },

    async getInfo(id) {
      const { data: { personName, phoneNumber, carNumber, carInfoId, carBrand, rechargeList }} = await cardInfo(id)
      this.form1 = {
        personName, phoneNumber, carNumber, carBrand, carInfoId
      }
      console.log(this.form1)
      this.form2 = {
        rechargeId: rechargeList[0].rechargeId,
        payTime: [rechargeList[0].cardStartDate, rechargeList[0].cardEndDate],
        paymentAmount: rechargeList[0].paymentAmount,
        paymentMethod: rechargeList[0].paymentMethod
      }
      console.log(this.form2)
    }
  }
}
</script>

  <style scoped lang="scss">
  .add-card {
    background-color: #f4f6f8;
    height: 100vh;

    .add-header {
      display: flex;
      align-items: center;
      padding: 0 20px;
      height: 64px;
      background-color: #fff;

      .left {
        span {
          margin-right: 4px;
        }
        .arrow{
          cursor: pointer;
        }
      }

      .right {
        text-align: right;
      }
    }

    .add-main {
      background: #f4f6f8;
      padding: 20px 130px;

      .form-container {
        background-color: #fff;

        .title {
          height: 60px;
          line-height: 60px;
          padding-left: 20px;
        }

        .form {
          margin-bottom: 20px;
          padding: 20px 65px 24px;

          .el-form {
            display: flex;
            flex-wrap: wrap;

            .el-form-item {
              width: 50%;
            }
          }
        }
      }
      .preview{
        img{
          width: 100px;
        }
      }
    }

    .add-footer {
      position: fixed;
      bottom: 0;
      width: 100%;
      padding: 24px 50px;
      color: #000000d9;
      font-size: 14px;
      background: #fff;
      text-align: center;
    }
  }
  </style>
